/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-19
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.code;

import org.springframework.jdbc.core.JdbcTemplate;

import com.pearlsoft.commons.utils.StringUtil;
import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.webapp.core.annotation.DataSetHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.dataset.spi.ContextDataSetHandler;

/**
 *描述：
 * 
 * 
 */
@DataSetHandlerComponent(name = "CodeDataSetHandler")
public class CodeDataSetHandler extends ContextDataSetHandler {

    public void handler(DataSet ds, Result result) throws Exception {

    }

    public void save(DataSet ds, Result result) throws Exception {
        JdbcTemplate template = this.getJdbcTemplate();
        boolean update = false;
        String id = (String) ds.getValue("ID");
        if (StringUtil.isNotEmpty(id)) {
            update = true;
        } else {
            id = UUIDUtil.get();
        }
        String codeType = (String) ds.getValue("CODE_TYPE");
        String codeDesc = (String) ds.getValue("CODE_DESC");
        String code = (String) ds.getValue("CODE");
        String codeOrder = (String) ds.getValue("CODE_ORDER");
        String codeText = (String) ds.getValue("CODE_TEXT");
        if (update) {
            template.update("UPDATE SYS_CODE SET CODE=?,CODE_TEXT=?,CODE_ORDER=? WHERE ID=?", new Object[] { code,
                    codeText, Integer.parseInt(codeOrder), id });
        } else {
            template.update(
                    "INSERT INTO SYS_CODE(ID,CODE_TYPE,CODE,CODE_TEXT,CODE_DESC,CODE_ORDER) VALUES(?,?,?,?,?,?)",
                    new Object[] { id, codeType, code, codeText, codeDesc, Integer.parseInt(codeOrder) });
        }
    }

    public void delete(DataSet ds, Result result) throws Exception {
        JdbcTemplate template = this.getJdbcTemplate();
        String id = (String) ds.getConditions().get("objId");
        template.update("DELETE FROM SYS_CODE WHERE ID=?", new Object[] { id });
    }
}
